package com.anyonavinfo.cpaddemo.data.weatherinfo.source.remote;

import android.content.Context;
import android.util.Log;


import com.anyonavinfo.cpaddemo.Application;

import com.anyonavinfo.cpaddemo.data.locationinfo.LocationInfo;
import com.anyonavinfo.cpaddemo.data.locationinfo.source.LocationInfoDataSource;
import com.anyonavinfo.cpaddemo.data.locationinfo.source.LocationInfoRepository;
import com.anyonavinfo.cpaddemo.data.weatherinfo.WeatherInfo;
import com.anyonavinfo.cpaddemo.data.weatherinfo.source.WeatherInfoDataSource;
import com.anyonavinfo.cpaddemo.utils.okhttputils.CacheType;
import com.anyonavinfo.cpaddemo.utils.okhttputils.JsonCallback;
import com.anyonavinfo.cpaddemo.utils.okhttputils.OKHttpUtils;
import com.google.gson.Gson;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;


/**
 * Created by ZuiC on 2017/2/3.
 */

<<<<<<< HEAD
public class WeatherInfoRemoteDataSource{

    private Context mContext;
    private OKHttpUtils okHttpUtils;
    private WeatherInfoDataSource.GetWeatherInfoCallBack callBack;
=======
public class WeatherInfoRemoteDataSource implements WeatherInfoDataSource{

    private Context mContext;
    private OKHttpUtils okHttpUtils;
    private GetWeatherInfoCallBack callBack;
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static final String URL = "http://wdapi.navinfo.com/semantics?userid=saicservice&key=4014ff5f06874340aa9d6c066b5c4368";


    private static WeatherInfoRemoteDataSource INSTANCE = null;

    public static WeatherInfoRemoteDataSource getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new WeatherInfoRemoteDataSource();
        }
        return INSTANCE;
    }

    private WeatherInfoRemoteDataSource(){
        mContext = Application.getContextObject();
    }


    private void getWeatherInfo(String city) {
        Gson gson = new Gson();
        WeatherRequestBean requestJson = new WeatherRequestBean();
        requestJson.setId("122121213322333");
        requestJson.setTime(System.currentTimeMillis() + "");
        requestJson.setText("今天天气好吗");
        requestJson.setCity(city);
        requestJson.setProvince(city);
        //requestJson.setLocation(longitude + "," + latitude);
        requestJson.setSpeed("0");
        requestJson.setBearing("0");
        requestJson.setIsNew(1);

        String json = gson.toJson(requestJson);
        RequestBody body = RequestBody.create(JSON,json);

        okHttpUtils = new OKHttpUtils.Builder(mContext).build();
        Request request = new Request.Builder().url(URL).post(body).build();
        okHttpUtils.request(request, CacheType.ONLY_NETWORK, jsonCallback);
    }



    private JsonCallback<WeatherInfoBean> jsonCallback = new JsonCallback<WeatherInfoBean>() {
        @Override
        public void onFailure(Call call, Exception e) {

        }
        @Override
        public void onResponse(Call call, WeatherInfoBean object) throws IOException {
            callBack.onGetWeatherInfo(WeatherInfoBeanHelper.parseWeatherInfoBean(object));
            callBack = null;
        }
    };

<<<<<<< HEAD

    public void getWeatherInfo(String city, final WeatherInfoDataSource.GetWeatherInfoCallBack callBack) {
=======
    @Override
    public void getWeatherInfo(String city, final GetWeatherInfoCallBack callBack) {
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
        this.callBack = callBack;
        getWeatherInfo(city);
    }


    private class WeatherRequestBean {
        /**
         * id : 122121213322333
         * time : 1463555547
         * text : 明天有雨吗
         * location : 116.440635,39.930549
         * speed : 0
         * bearing : 0
         * isNew : 1
         */
        private String id;
        private String city;
        private String province;
        private String time;
        private String text;
        private String location;
        private String speed;
        private String bearing;
        private int isNew;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getSpeed() {
            return speed;
        }

        public void setSpeed(String speed) {
            this.speed = speed;
        }

        public String getBearing() {
            return bearing;
        }

        public void setBearing(String bearing) {
            this.bearing = bearing;
        }

        public int getIsNew() {
            return isNew;
        }

        public void setIsNew(int isNew) {
            this.isNew = isNew;
        }
    }
}
